# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=directxtex
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Version from CMakeLists.txt
pkgver=1.9.9
_tag=jun2023
pkgrel=1
pkgdesc="DirectXTex texture processing library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://go.microsoft.com/fwlink/?LinkId=248926/'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-directx-headers" 
         "${MINGW_PACKAGE_PREFIX}-directxmath")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${_tag}.tar.gz"::"https://github.com/microsoft/DirectXTex/archive/$_tag.tar.gz")
sha256sums=('51f0ff3bee0d1015c110e0c92ebdd9704aa6acd91185328fd92f10b9558f4c62')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  #DBUILD_DX11 requires fxc.exe but that's not available.
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_DX11=OFF \
      -DBUILD_DX12=ON \
      -DBUILD_TOOLS=ON \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      ../${_realname}-${_tag}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${_tag}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
